package com.niiwoo.civet.user.service.duboo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.trade.constant.MqConstant;
import com.niiwoo.civet.trade.dto.request.contract.WithholdContractSignMQDTO;
import com.niiwoo.civet.trade.service.project.ProjectDubboService;
import com.niiwoo.civet.user.dao.entity.DataStatus;
import com.niiwoo.civet.user.enums.UserDataStatusEnum;
import com.niiwoo.civet.user.enums.UserDataTypeEnum;
import com.niiwoo.civet.user.service.FadadaDubboService;
import com.niiwoo.civet.user.service.local.UserStatusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * @author: liaozuliang
 * @date: 2018/5/17
 */
@Slf4j
@Service(version = "1.0.0")
public class FadadaDubboServiceImpl implements FadadaDubboService {

    @Reference(version = "1.0.0")
    private ProjectDubboService projectDubboService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private UserStatusService userStatusService;

    @Override
    public void signRepayAgreementContractForOldUser(String userId) {
        DataStatus dataStatus = userStatusService.getByUserIdAndType(userId, UserDataTypeEnum.WITHHOLD_CONTRACT_SIGN);
        if (dataStatus != null && UserDataStatusEnum.WithholdContractSignStatus.ALREADY_SIGN.getStatus().equals(dataStatus.getDataStatus())) {
            log.info("用户[userId:{}]已签署《还款服务委托书》，不需要重新签署", userId);
            return;
        }

        Long projectId = projectDubboService.isNeedSignRepayAgreementContractForOldUser(userId);
        if (projectId != null) {
            // 先修改签约状态为已经签约
            updateWithholdContractSignStatus(userId, UserDataStatusEnum.WithholdContractSignStatus.ALREADY_SIGN);

            // 发送mq消息通知签约
            WithholdContractSignMQDTO withholdContractSignMQDTO = new WithholdContractSignMQDTO();
            withholdContractSignMQDTO.setUserId(userId);
            withholdContractSignMQDTO.setProjectId(projectId);

            rabbitTemplate.convertAndSend(MqConstant.FADADA_CONTRACT_EXCHANGE, MqConstant.FADADA_WITHHOLD_CONTRACT_ROUTE, withholdContractSignMQDTO);
            log.info("老用户[userId:{}, projectId:{}]补签《还款服务委托书》MQ消息发送成功", userId, projectId);
        }
    }

    /**
     * 更新《还款服务委托书》合同签约状态
     * @param userId
     * @param withholdContractSignStatus
     */
    private void updateWithholdContractSignStatus(String userId, UserDataStatusEnum.WithholdContractSignStatus withholdContractSignStatus) {
        Date now = new Date();
        DataStatus dataStatus = new DataStatus();

        dataStatus.setUserId(userId);
        dataStatus.setDataType(UserDataTypeEnum.WITHHOLD_CONTRACT_SIGN.getValue());
        dataStatus.setDataStatus(withholdContractSignStatus.getStatus());
        dataStatus.setAuthTime(now);
        dataStatus.setCreateTime(now);

        userStatusService.saveDataStatusByUserIdAndType(dataStatus);
    }
}
